package de.pyranja.money;

import java.math.BigDecimal;
import java.util.Currency;

import de.pyranja.money.core.BaseMoney;

/**
 * Instances of this class represent a fixed amount of money in a set currency
 * and offer operations on this amount.
 * 
 * @author Chris Borckholder
 */
public class Money extends BaseMoney {

	private final MoneyCalculator calculator;
	
	/**
	 * Full constructor
	 */
	public Money(BigDecimal amount, Currency currency,
			MoneyCalculator calculator) {
		super(amount, currency);
		this.calculator = calculator;
	}

	/**
	 * Internal constructor from BaseMoney instances
	 */
	protected Money(BaseMoney base, MoneyCalculator calculator) {
		this(base.getAmount(), base.getCurrency(), calculator);
	}

	/**
	 * Add another amount to this amount.
	 * @param that another amount of money to be added
	 * @return a new instance as sum of this and that
	 */
	public Money add(Money that) {
		final BaseMoney result = calculator.add(this, that);
		return new Money(result, calculator);
	}

	/**
	 * Subtract another amount of money from this amount
	 * @param that another amount to be subtracted
	 * @return a new instance representing the difference between this and that
	 */
	public Money sub(Money that) {
		final BaseMoney result = calculator.subtract(this, that);
		return new Money(result, calculator);
	}

	/**
	 * Multiply by <tt>double</tt> factor.
	 */
	public Money times(double factor) {
		final BaseMoney result = calculator.multiply(this,
				BigDecimal.valueOf(factor));
		return new Money(result, calculator);
	}

	/**
	 * Multiply by <tt>long</tt> factor.
	 */
	public Money times(long factor) {
		final BaseMoney result = calculator.multiply(this,
				BigDecimal.valueOf(factor));
		return new Money(result, calculator);
	}
}